<?php
include("connect.php");
?>

<?php
$type = $_REQUEST["type"];

$folder_position = getcwd().DIRECTORY_SEPARATOR;

$title = $_REQUEST["title"];
$author = $_REQUEST["author"];
$price = $_REQUEST["price"];
$binding = $_REQUEST["binding"];
$cover = $_REQUEST["cover"];
$page = $_REQUEST["page"];
$all = $_REQUEST["all"];
$userid = $_SESSION['userid'];
$category = $_REQUEST['course'];
$major1 = $_REQUEST['major1'];
$major2 = $_REQUEST['major2'];
$bookid =$_REQUEST['bookid']; 
$userbookid = $_REQUEST['userbookid']; 



$sql = "select jcu_id from M_USER where user_id = ".$_SESSION["userid"];
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
	$jcuid = $row[0];
}
if($type =='edit')
{

	if($_FILES["img1"]["error"]>0)
	{
	
	}else
	{
		$link1 = $jcuid."_".strtotime('now')."_".$_FILES["img1"]["name"];
		$target_img1 = $folder_position . "images/upload/".$link1;
		move_uploaded_file($_FILES["img1"]["tmp_name"],$target_img1);
	}	

	if($_FILES["img2"]["error"]>0)
	{
	
	}else
	{
		$link2 = $jcuid."_".strtotime('now')."_".$_FILES["img2"]["name"];
		$target_img2 = $folder_position . "images/upload/".$link2;
		move_uploaded_file($_FILES["img2"]["tmp_name"],$target_img2);
	}	
	if($_FILES["img3"]["error"]>0)
	{
	
	}else
	{
		$link3 = $jcuid."_".strtotime('now')."_".$_FILES["img3"]["name"];
		$target_img3 = $folder_position . "images/upload/".$link3;
		move_uploaded_file($_FILES["img3"]["tmp_name"],$target_img3);
	}	

	$sql = "update  M_BOOK set book_title='".$title."' ,book_author='".$author."' ";
	$sql .="where book_id= ".$bookid;
	mysql_query($sql);
	echo $sql."<br>";
	$sql= "update T_USER_BOOK set price=".$price." , condition1=".$binding." ,condition2=".$cover." ,condition3=".$page." ,condition4=".$all;
	$sql .= " where user_book_id=".$userbookid;
	mysql_query($sql);
	echo $sql."<br>";


if(isset($target_img1))
{
	$sql = "delete from T_BOOK_PHOTO where bookphoto_series=1 and user_book_id=".$user_book_id;
		mysql_query($sql);
	$sql = "udpate T_BOOK_PHOTO set bookphoto_link=".$link1	;
	$sql .=" where user_book_id=".$user_book_id." and bookphoto_series=1";
	mysql_query($sql);
}

if(isset($target_img2))
{
$sql = "delete from T_BOOK_PHOTO where bookphoto_series=2 and user_book_id=".$user_book_id;
		mysql_query($sql);
	$sql = "udpate T_BOOK_PHOTO set bookphoto_link=".$link2	;
	$sql .=" where user_book_id=".$user_book_id." and bookphoto_series=2";
	mysql_query($sql);
}

if(isset($target_img3))
{
$sql = "delete from T_BOOK_PHOTO where bookphoto_series=3 and user_book_id=".$user_book_id;
		mysql_query($sql);
	$sql = "udpate T_BOOK_PHOTO set bookphoto_link=".$link3;
	$sql .=" where user_book_id=".$user_book_id." and bookphoto_series=3";
	mysql_query($sql);

}
$sql = "delete from T_BOOK_CATEGORY where book_id=".$bookid;
mysql_query($sql);
echo $sql."<br>";
$sql = "insert into T_BOOK_CATEGORY(book_id,category_detail_id) ";
$sql .= "values ($bookid,$major1)";
mysql_query($sql);
echo $sql."<br>";

if($major2 != 0 )
{
	$sql = "insert into T_BOOK_CATEGORY(book_id,category_detail_id) ";
	$sql .= "values ($bookid,$major2)";
mysql_query($sql);
}
$page = "book_detail.php?userbookid=".$userbookid."&type=sale";
?>

<script type="text/javascript">
<!--
alert("edit success!");
window.location = "<?php echo $page.".php"; ?>"
//-->
</script>

<?php
}else
{
if($_FILES["img1"]["error"]>0)
{
	echo '<script>';
	echo "alert('Please upload the photo of your book!');";
	echo 'window.location = "sell_book.php"';
	echo '</script>';
}else
{
	$link1 = $jcuid."_".strtotime('now')."_".$_FILES["img1"]["name"];
	$target_img1 = $folder_position . "images/upload/".$link1;
	move_uploaded_file($_FILES["img1"]["tmp_name"],$target_img1);
}	

if($_FILES["img2"]["error"]>0)
{
//echo '<script>';
//	echo "alert('Please upload the photo of your book!');";
//	echo 'window.location = "sell_book.php"';
//	echo '</script>';
}else
{
	$link2 = $jcuid."_".strtotime('now')."_".$_FILES["img2"]["name"];
	$target_img2 = $folder_position . "images/upload/".$link2;
	move_uploaded_file($_FILES["img2"]["tmp_name"],$target_img2);
}	
if($_FILES["img3"]["error"]>0)
{
//echo '<script>';
//	echo "alert('Please upload the photo of your book!');";
//	echo 'window.location = "sell_book.php"';
//	echo '</script>';
}else
{
	$link3 = $jcuid."_".strtotime('now')."_".$_FILES["img3"]["name"];
	$target_img3 = $folder_position . "images/upload/".$link3;
	move_uploaded_file($_FILES["img3"]["tmp_name"],$target_img3);
}	
	
}
     


if($bookid == 0)
{
	$sql = "insert into M_BOOK(book_title,book_author,ISBN) ";
	$sql .="values ('$title','$author','123-456-789')";
	mysql_query($sql);
	$sql = "SELECT book_id FROM M_BOOK ORDER BY book_id DESC LIMIT 1";
	$result = mysql_query($sql);
	while($row = mysql_fetch_array($result))
	{
		$bookid = $row[0];
	}
}

$sql= "insert into T_USER_BOOK(user_id,book_id,price,condition1,condition2,condition3,condition4) ";
$sql .= "values ($userid,$bookid,$price,$binding,$cover,$page,$all)";
mysql_query($sql);

$sql = "SELECT user_book_id FROM T_USER_BOOK ORDER BY user_book_id DESC LIMIT 1";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
	$userbookid = $row[0];
}

if(isset($target_img1))
{
	$sql = "insert into T_BOOK_PHOTO(user_book_id,bookphoto_link,bookphoto_series)";
	$sql .="values(".$userbookid.",'".$link1."',1)";
	mysql_query($sql);
}

if(isset($target_img2))
{
	$sql = "insert into T_BOOK_PHOTO(user_book_id,bookphoto_link,bookphoto_series)";
	$sql .="values(".$userbookid.",'".$link2."',2)";
	mysql_query($sql);
}

if(isset($target_img3))
{
	$sql = "insert into T_BOOK_PHOTO(user_book_id,bookphoto_link,bookphoto_series)";
	$sql .="values(".$userbookid.",'".$link3."',3)";
	mysql_query($sql);
}


$sql = "insert into T_BOOK_CATEGORY(book_id,category_detail_id) ";
$sql .= "values ($bookid,$major1)";
mysql_query($sql);

if($major2 != 0 )
{
	$sql = "insert into T_BOOK_CATEGORY(book_id,category_detail_id) ";
	$sql .= "values ($bookid,$major2)";
mysql_query($sql);
}
$page = "book_detail.php?userbookid=".$userbookid."&type=sale";
?>

<script type="text/javascript">
<!--
window.location = "<?php echo $page.".php"; ?>"
//-->
</script>
